package com.shangdiao.sd_server.p_client.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shangdiao.sd_server.constant.Constant;
import com.shangdiao.sd_server.constant.ErrorCode;
import com.shangdiao.sd_server.generator.pojo.SdCertificate;
import com.shangdiao.sd_server.generator.pojo.SdCertificateExample;
import com.shangdiao.sd_server.generator.pojo.SdEduExp;
import com.shangdiao.sd_server.generator.pojo.SdEduExpExample;
import com.shangdiao.sd_server.generator.pojo.SdInExp;
import com.shangdiao.sd_server.generator.pojo.SdInExpExample;
import com.shangdiao.sd_server.generator.pojo.SdLabel;
import com.shangdiao.sd_server.generator.pojo.SdLabelExample;
import com.shangdiao.sd_server.generator.pojo.SdOutExp;
import com.shangdiao.sd_server.generator.pojo.SdOutExpExample;
import com.shangdiao.sd_server.generator.pojo.SdPerson;
import com.shangdiao.sd_server.generator.pojo.SdPersonBase;
import com.shangdiao.sd_server.generator.pojo.SdPersonExample;
import com.shangdiao.sd_server.generator.pojo.SdPersonPhoto;
import com.shangdiao.sd_server.generator.pojo.SdPersonPhotoExample;
import com.shangdiao.sd_server.pojo.ResponseMsg;
import com.shangdiao.sd_server.pojo.Row;
import com.shangdiao.sd_server.service.DicService;
import com.shangdiao.sd_server.service.PersonService;
import com.shangdiao.sd_server.service.RedisService;
import com.shangdiao.sd_server.service.SequenceService;
import com.shangdiao.sd_server.util.DateUtil;
import com.shangdiao.sd_server.util.RequestUtil;
import com.shangdiao.sd_server.util.StringUtil;

@Controller
@RequestMapping("/open/person")
public class PersonInfoController {
	
	@Autowired
	DicService dicService;
	@Autowired
	PersonService personService;
	@Autowired
	RedisService redisService;
	@Autowired
	SequenceService sequenceService;
	
	
	@RequestMapping(value = { "/getInfo.json" })
	@ResponseBody
	public ResponseMsg checktel(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "personId");
			if(personId == 0){
				return new ResponseMsg("缺少必要参数！", ErrorCode.ERROR, null);
			}
			SdPersonBase personBase = personService.selectPersonBaseById(personId); 
			SdPerson person = personService.selectPersonById(personId);
			Row result = new Row();
			result.put("identityFlag", "p");//身份标识
			result.put("personName", person.getPersonName());//姓名
			result.put("personGender", person.getPersonGender());//性别
			result.put("birthday", DateUtil.DateToString(person.getBirthday(), DateUtil.LONG_DATE_FORMAT));//出生日期
			result.put("tel", person.getTel());//手机
			result.put("email", person.getEmail());//邮箱
			result.put("districtId", person.getDistrictId());//城市id 
			result.put("district", person.getDistrict());//城市
			result.put("postFlag", person.getPostFlag());//职位类型标识 1实习2兼职
			result.put("dicFieldId", person.getDicFieldId());//选择职位id 
			result.put("dicFieldName", person.getDicFieldName());//选择职位
			result.put("universityId", person.getUniversityId());//学校
			result.put("university", person.getUniversity());//学校
			result.put("college", person.getCollege());//学院
			result.put("major", person.getMajor());//专业
			result.put("admissionDate", DateUtil.DateToString(person.getAdmissionDate(), DateUtil.FORMAT_MONTH));//入学时间
			result.put("dicEducationId", person.getDicEducationId());//学历
			result.put("dicEducationName", person.getDicEducationName());//学历
			result.put("isInvited", person.getIsInvited());//是否接受邀请
			//logo
			String logo= personBase.getLogo();
			if(StringUtil.isBlank(logo)){
				result.put("logo", "");
			}else{
				result.put("logo", Constant.DOMAIN + logo);
			}
			//学生证
			String card = person.getCard();
			if(StringUtil.isBlank(card)){
				result.put("card", "");
			}else{
				result.put("card", Constant.DOMAIN + card);
			}
			//教育经历
			SdEduExpExample eduExpExample = new SdEduExpExample();
			eduExpExample.or().andPersonIdEqualTo(personId).andDelEqualTo(Constant.DEL_NO);
			List<SdEduExp> eduExpList = personService.selectEduExpByExample(eduExpExample);
			List<Row> eduExpRow = new ArrayList<Row>();
			for(SdEduExp eduExp : eduExpList){
				Row exp = new Row();
				exp.put("expId", eduExp.getExpId());
				exp.put("university", eduExp.getUniversity());
				exp.put("major", eduExp.getMajor());
				exp.put("during", DateUtil.DateToString(eduExp.getStartDate(), DateUtil.FORMAT_EIGHT) + "-" + DateUtil.DateToString(eduExp.getEndDate(), DateUtil.FORMAT_EIGHT));
				exp.put("startDate", DateUtil.DateToString(eduExp.getStartDate(), DateUtil.FORMAT_MONTH));
				exp.put("endDate", DateUtil.DateToString(eduExp.getEndDate(), DateUtil.FORMAT_MONTH));
				eduExpRow.add(exp);
			}
			result.put("eduExp", eduExpRow);
			//实践经历
			SdOutExpExample outExpExample = new SdOutExpExample();
			outExpExample.or().andPersonIdEqualTo(personId).andDelEqualTo(Constant.DEL_NO);
			List<SdOutExp> outExpList = personService.selectOutExpByExample(outExpExample);
			List<Row> outExpRow = new ArrayList<Row>();
			for(SdOutExp outExp : outExpList){
				Row exp = new Row();
				exp.put("expId", outExp.getExpId());
				exp.put("expName", outExp.getExpName());
				exp.put("desc", outExp.getDescription());
				exp.put("during", DateUtil.DateToString(outExp.getStartDate(), DateUtil.FORMAT_EIGHT) + "-" + DateUtil.DateToString(outExp.getEndDate(), DateUtil.FORMAT_EIGHT));
				exp.put("startDate", DateUtil.DateToString(outExp.getStartDate(), DateUtil.FORMAT_MONTH));
				exp.put("endDate", DateUtil.DateToString(outExp.getEndDate(), DateUtil.FORMAT_MONTH));
				outExpRow.add(exp);
			}
			result.put("outExp", outExpRow);
			//校内经历
			SdInExpExample inExpExample = new SdInExpExample();
			inExpExample.or().andPersonIdEqualTo(personId).andDelEqualTo(Constant.DEL_NO);
			List<SdInExp> inExpList = personService.selectInExpByExample(inExpExample);
			List<Row> inExpRow = new ArrayList<Row>();
			for(SdInExp inExp : inExpList){
				Row exp = new Row();
				exp.put("expId", inExp.getExpId());
				exp.put("expName", inExp.getExpName());
				exp.put("desc", inExp.getDescription());
				exp.put("during", DateUtil.DateToString(inExp.getStartDate(), DateUtil.FORMAT_EIGHT) + "-" + DateUtil.DateToString(inExp.getEndDate(), DateUtil.FORMAT_EIGHT));
				exp.put("startDate", DateUtil.DateToString(inExp.getStartDate(), DateUtil.FORMAT_MONTH));
				exp.put("endDate", DateUtil.DateToString(inExp.getEndDate(), DateUtil.FORMAT_MONTH));
				inExpRow.add(exp);
			}
			result.put("inExp", inExpRow);
			//证书技能
			SdCertificateExample certificateExample = new SdCertificateExample();
			certificateExample.or().andPersonIdEqualTo(personId).andDelEqualTo(Constant.DEL_NO);
			List<SdCertificate> certificateList = personService.selectCertificateByExample(certificateExample);
			List<Row> certificateRow = new ArrayList<Row>();
			for(SdCertificate certificate : certificateList){
				Row exp = new Row();
				exp.put("certId", certificate.getCertId());
				exp.put("certUrl", Constant.DOMAIN + certificate.getCertUrl());
				certificateRow.add(exp);
			}
			result.put("certificate", certificateRow);
			//个人标签
			SdLabelExample labelExample = new SdLabelExample();
			labelExample.or().andPersonIdEqualTo(personId).andDelEqualTo(Constant.DEL_NO);
			List<SdLabel> labelList = personService.selectLabelByExample(labelExample);
			List<Row> labelRow = new ArrayList<Row>();
			for(SdLabel label : labelList){
				Row exp = new Row();
				exp.put("labelId", label.getLabelId());
				exp.put("dicLabelName", label.getDicLabelName());
				labelRow.add(exp);
			}
			result.put("label", labelRow);
			//自我评价
			result.put("selfEvaluation", person.getSelfEvaluation());
			//个人风采
			SdPersonPhotoExample personPhotoExample = new SdPersonPhotoExample();
			personPhotoExample.or().andPersonIdEqualTo(personId).andDelEqualTo(Constant.DEL_NO);
			List<SdPersonPhoto> personPhotoList = personService.selectPersonPhotoByExample(personPhotoExample);
			List<Row> personPhotoRow = new ArrayList<Row>();
			for(SdPersonPhoto personPhoto : personPhotoList){
				Row exp = new Row();
				exp.put("personPhotoId", personPhoto.getPersonPhotoId());
				exp.put("personPhotoUrl", Constant.DOMAIN + personPhoto.getPersonPhotoUrl());
				personPhotoRow.add(exp);
			}
			result.put("personPhoto", personPhotoRow);
			return new ResponseMsg("查询成功", ErrorCode.OK, result);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("服务器出错", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	
	@RequestMapping(value = { "/selectResume.json" })
	@ResponseBody
	public ResponseMsg selectResume(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer dicFieldId = RequestUtil.getInt(request, "dicFieldId");
			Integer postFlag = RequestUtil.getInt(request, "postFlag");
			Integer personGender = RequestUtil.getInt(request, "personGender");
			Integer dicEducationId = RequestUtil.getInt(request, "dicEducationId");
			int curPage = RequestUtil.getInt(request, "curPage", Constant.DEFAULT_CUR_PAGE);
			int pageSize = RequestUtil.getInt(request, "pageSize", Constant.DEFAULT_PAGE_SIZE);
			if(postFlag == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			int start = (curPage - 1) * pageSize;
			SdPersonExample example = new SdPersonExample();
			if(personGender != null && dicEducationId != null && dicFieldId != null){
				example.or().andPostFlagEqualTo(postFlag)
				.andDicFieldIdEqualTo(dicFieldId)
				.andIsInvitedEqualTo(Constant.STATUS_ON)//接受邀约
				//信息完整校验 －start
				.andPersonNameNotEqualTo("")
				.andPostFlagIsNotNull()
				.andPersonGenderIsNotNull()
				.andBirthdayIsNotNull()
				.andEmailIsNotNull()
				.andDistrictIdIsNotNull()
				.andDicFieldIdIsNotNull()
				.andUniversityIdIsNotNull()
				.andDicEducationIdIsNotNull()
				//信息完整校验 －end
				.andPersonGenderEqualTo(personGender)
				.andDicEducationIdEqualTo(dicEducationId);
			}else if(personGender == null && dicEducationId != null && dicFieldId != null){
				example.or().andPostFlagEqualTo(postFlag)
				.andIsInvitedEqualTo(Constant.STATUS_ON)//接受邀约
				//信息完整校验 －start
				.andPersonNameNotEqualTo("")
				.andPostFlagIsNotNull()
				.andPersonGenderIsNotNull()
				.andBirthdayIsNotNull()
				.andEmailIsNotNull()
				.andDistrictIdIsNotNull()
				.andDicFieldIdIsNotNull()
				.andUniversityIdIsNotNull()
				.andDicEducationIdIsNotNull()
				//信息完整校验 －end
				.andDicFieldIdEqualTo(dicFieldId)
				.andDicEducationIdEqualTo(dicEducationId);
			}else if(personGender != null && dicEducationId == null && dicFieldId != null){
				example.or().andPostFlagEqualTo(postFlag)
				.andIsInvitedEqualTo(Constant.STATUS_ON)//接受邀约
				//信息完整校验 －start
				.andPersonNameNotEqualTo("")
				.andPostFlagIsNotNull()
				.andPersonGenderIsNotNull()
				.andBirthdayIsNotNull()
				.andEmailIsNotNull()
				.andDistrictIdIsNotNull()
				.andDicFieldIdIsNotNull()
				.andUniversityIdIsNotNull()
				.andDicEducationIdIsNotNull()
				//信息完整校验 －end
				.andDicFieldIdEqualTo(dicFieldId)
				.andPersonGenderEqualTo(personGender);
			}else if(personGender != null && dicEducationId != null && dicFieldId == null){
				example.or().andPostFlagEqualTo(postFlag)
				.andIsInvitedEqualTo(Constant.STATUS_ON)//接受邀约
				//信息完整校验 －start
				.andPersonNameNotEqualTo("")
				.andPostFlagIsNotNull()
				.andPersonGenderIsNotNull()
				.andBirthdayIsNotNull()
				.andEmailIsNotNull()
				.andDistrictIdIsNotNull()
				.andDicFieldIdIsNotNull()
				.andUniversityIdIsNotNull()
				.andDicEducationIdIsNotNull()
				//信息完整校验 －end
				.andPersonGenderEqualTo(personGender)
				.andDicEducationIdEqualTo(dicEducationId);
			}else if(personGender != null && dicEducationId == null && dicFieldId == null){
				example.or().andPostFlagEqualTo(postFlag)
				.andIsInvitedEqualTo(Constant.STATUS_ON)//接受邀约
				//信息完整校验 －start
				.andPersonNameNotEqualTo("")
				.andPostFlagIsNotNull()
				.andPersonGenderIsNotNull()
				.andBirthdayIsNotNull()
				.andEmailIsNotNull()
				.andDistrictIdIsNotNull()
				.andDicFieldIdIsNotNull()
				.andUniversityIdIsNotNull()
				.andDicEducationIdIsNotNull()
				//信息完整校验 －end
				.andPersonGenderEqualTo(personGender);
			}else if(personGender == null && dicEducationId != null && dicFieldId == null){
				example.or().andPostFlagEqualTo(postFlag)
				.andIsInvitedEqualTo(Constant.STATUS_ON)//接受邀约
				//信息完整校验 －start
				.andPersonNameNotEqualTo("")
				.andPostFlagIsNotNull()
				.andPersonGenderIsNotNull()
				.andBirthdayIsNotNull()
				.andEmailIsNotNull()
				.andDistrictIdIsNotNull()
				.andDicFieldIdIsNotNull()
				.andUniversityIdIsNotNull()
				.andDicEducationIdIsNotNull()
				//信息完整校验 －end
				.andDicEducationIdEqualTo(dicEducationId);
			}else if(personGender == null && dicEducationId == null && dicFieldId != null){
				example.or().andPostFlagEqualTo(postFlag)
				.andIsInvitedEqualTo(Constant.STATUS_ON)//接受邀约
				//信息完整校验 －start
				.andPersonNameNotEqualTo("")
				.andPostFlagIsNotNull()
				.andPersonGenderIsNotNull()
				.andBirthdayIsNotNull()
				.andEmailIsNotNull()
				.andDistrictIdIsNotNull()
				.andDicFieldIdIsNotNull()
				.andUniversityIdIsNotNull()
				.andDicEducationIdIsNotNull()
				//信息完整校验 －end
				.andDicFieldIdEqualTo(dicFieldId);
			}  
			example.setOrderByClause("create_time limit " + start + "," + pageSize);
			List<SdPerson> personList = personService.selectPersonByExample(example);
			List<Row> resultList = new ArrayList<Row>(); 
			for(SdPerson person : personList){
				Row result = new Row();
				Integer personId = person.getPersonId();
				SdPersonBase personBase = personService.selectPersonBaseById(personId);
				String logo = personBase.getLogo();
				if(StringUtil.isBlank(logo)){
					result.put("logo", "");
				}else{
					result.put("logo", Constant.DOMAIN + logo);
				}
				result.put("personId", person.getPersonId());
				result.put("personName", person.getPersonName());
				result.put("personGender", person.getPersonGender() == 0 ? "女" : "男");
				Date birthday = person.getBirthday();
				if(birthday != null){
					result.put("personAge", DateUtil.yearDiffCurr(DateUtil.DateToString(birthday, DateUtil.LONG_DATE_FORMAT)));
				}else{
					result.put("personAge", "");
				}
				result.put("postFlag", person.getPostFlag());
				result.put("dicFieldName", person.getDicFieldName());
				result.put("university", person.getUniversity());
				result.put("major", person.getMajor());
				result.put("dicEducationName", person.getDicEducationName());
				result.put("district", person.getDistrict());
				resultList.add(result);
			}
			return new ResponseMsg("查询成功", ErrorCode.OK, resultList);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("服务器出错", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/isCompleted.json" })
	@ResponseBody
	public ResponseMsg isCompleted(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "personId");
			if(personId == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			Row result = new Row();
			SdPerson person = personService.selectPersonById(personId);
			if(person == null){
				return new ResponseMsg("用户不存在", ErrorCode.ERROR, null);
			}
			if(StringUtil.isBlank(person.getPersonName())){
				result.put("isCompleted", false);
				return new ResponseMsg("查询成功", ErrorCode.OK, result);
			}
			if(person.getPostFlag() == null || person.getPostFlag() == 0){
				result.put("isCompleted", false);
				return new ResponseMsg("查询成功", ErrorCode.OK, result);
			}
			if(person.getPersonGender() == null){
				result.put("isCompleted", false);
				return new ResponseMsg("查询成功", ErrorCode.OK, result);
			}
			if(person.getBirthday() == null){
				result.put("isCompleted", false);
				return new ResponseMsg("查询成功", ErrorCode.OK, result);
			}
			if(StringUtil.isBlank(person.getEmail())){
				result.put("isCompleted", false);
				return new ResponseMsg("查询成功", ErrorCode.OK, result);
			}
			if(person.getDistrictId() == null || person.getDistrictId() == 0){
				result.put("isCompleted", false);
				return new ResponseMsg("查询成功", ErrorCode.OK, result);
			}
			if(person.getDicFieldId() == null || person.getDicFieldId() == 0){
				result.put("isCompleted", false);
				return new ResponseMsg("查询成功", ErrorCode.OK, result);
			}
			if(person.getUniversityId() == null || person.getUniversityId() == 0){
				result.put("isCompleted", false);
				return new ResponseMsg("查询成功", ErrorCode.OK, result);
			}
			if(person.getDicEducationId() == null || person.getDicEducationId() == 0){
				result.put("isCompleted", false);
				return new ResponseMsg("查询成功", ErrorCode.OK, result);
			}
			result.put("isCompleted", true);
			result.put("isInvited", person.getIsInvited());//是否接受邀请
			return new ResponseMsg("查询成功", ErrorCode.OK, result);
		}catch(Exception e){
				e.printStackTrace();
				return new ResponseMsg("服务器出错", ErrorCode.SERVER_ERROR, null);
			}
		}	
	
	@RequestMapping(value = { "/selectHotLabel.json" })
	@ResponseBody
	public ResponseMsg selectHotLabel(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer postFlag = RequestUtil.getInt(request, "postFlag", Constant.POST_FLAG_INTERNSHIP);//默认实习
			List<Row> resultList = new ArrayList<Row>();
			if(postFlag == Constant.POST_FLAG_INTERNSHIP){
				resultList = personService.selectInternshipHotLabel();
			}else if(postFlag == Constant.POST_FLAG_PARTTIME){
				resultList = personService.selectParttimeHotLabel();
			}
			return new ResponseMsg("查询成功", ErrorCode.OK, resultList);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("服务器出错", ErrorCode.SERVER_ERROR, null);
		}
	}	
	
	
	public static void main(String[] args) {
		
	}
	
}
